<?php
/**
 * UserPost.php
 * Created by HappyOnion.
 * User: yang
 * Date: 2017/12/8 Time: 14:51
 */

namespace backend\models;

use Exception;
use webvimark\modules\UserManagement\components\AuthHelper;
use Yii;
use yii\rbac\DbManager;
use backend\models\AbstractItem;

class UserPost extends AbstractItem
{
    const ITEM_TYPE = self::TYPE_POST;

    public $id = null;

    public function actionCreate()
    {
        $model = new UserPost();
        $model->scenario = 'webInput';

        if ( $model->load(Yii::$app->request->post()) && $model->save() )
        {

            return $this->redirect(['view', 'id'=>$model->name]);
        }
        return $this->renderIsAjax('create', compact('model'));
    }




    /**
     * Assign route to permission and create them if they don't exists
     * Helper mainly for migrations
     *
     * @param string       $permissionName
     * @param array|string $routes
     * @param null|string  $permissionDescription
     * @param null|string  $groupCode
     *
     * @throws \InvalidArgumentException
     * @return true|static|string
     */
    public static function assignRoutes($permissionName, $routes, $permissionDescription = null, $groupCode = null)
    {
        $permission = static::findOne(['name' => $permissionName]);
        $routes = (array)$routes;

        if ( !$permission )
        {
            $permission = static::create($permissionName, $permissionDescription, $groupCode);

            if ( $permission->hasErrors() )
            {
                return $permission;
            }
        }

        foreach ($routes as $route)
        {
            $route = '/' . ltrim($route, '/');
            try
            {
                Yii::$app->db->createCommand()
                    ->insert(Yii::$app->getModule('user-management')->auth_item_child_table, [
                        'parent' => $permission->name,
                        'child'  => $route,
                    ])->execute();
            }
            catch (Exception $e)
            {
                // Don't throw Exception because this permission may already have this route,
                // so just go to the next route
            }
        }

        AuthHelper::invalidatePermissions();

        return true;
    }
}
